var winW = $(window).width(),
	winH = $(window).height(),
	isloading = true;

$(document).ready(function(){
	var speed = 5,
		rotate_c = 0,
		rotate_lr = 360;
		
	function setrotate_z(value){
		$('#loading .rotate .circle_r').css({
			"-webkit-transform":"rotateZ("+ value +"deg)",
			"-moz-transform":"rotateZ("+ value +"deg)",
			"-ms-transform":"rotateZ("+ value +"deg)",
			"-o-transform":"rotateZ("+ value +"deg)",
			"transform":"rotateZ("+ value +"deg)"
		});
		$('#loading .rotate .circle_l').css({
			"-webkit-transform":"rotateZ("+ value +"deg)",
			"-moz-transform":"rotateZ("+ value +"deg)",
			"-ms-transform":"rotateZ("+ value +"deg)",
			"-o-transform":"rotateZ("+ value +"deg)",
			"transform":"rotateZ("+ value +"deg)"
		});
	}
	function setrotate_c(value_c){
		$('#loading .rotate .circle_c').css({
			"-webkit-transform":"rotateZ("+ value_c +"deg)",
			"-moz-transform":"rotateZ("+ value_c +"deg)",
			"-ms-transform":"rotateZ("+ value_c +"deg)",
			"-o-transform":"rotateZ("+ value_c +"deg)",
			"transform":"rotateZ("+ value_c +"deg)"
		});
	}
	function circle(){
		if(isloading){
			var num = Math.round(Math.random()*3+7);
			if(rotate_c < 360){
				setrotate_c(rotate_c);
				rotate_c += speed;
			}else if(rotate_c >= 360){
				rotate_c = 0;
			}
			if(rotate_lr){
				setrotate_z(rotate_lr);
				rotate_lr -= speed;
			}else if(rotate_lr <= 0){
				rotate_lr = 360;
			}
			setTimeout(function(){
				circle();
				$('#loading .loadbg').css({"opacity":num/10});
			},50);
		}
	}
	circle();
	
	var img = new Image();		
	img.src = "./images/load_w.png";
	
	if(img.complete){
		imageLoaded();
	}else{
		img.onload = imageLoaded();
	};
	
	function imageLoaded(){
		$('#loading .loadingwarp').show();
	}
	//$('#loading .loadingwarp').show();
	
	
		
});




$('.mini_nav').on("click", function(){
	if($('.menu').is(":visible")){
		$('.menu').slideUp("slow");
		$(this).removeClass('open');
	}else{
		$(this).addClass('open');
		$('.menu').slideDown("slow");
	}
});

$('.menuwarp .m_l').on("click", function(){
	if($(this).find('ul').is(":visible")){
		$(this).find('ul').hide();
	}else{
		$(this).find('ul').show();
	}
});



var pages = null,
	timeout = false,
	critical = 140;
var curPage = 0,
	maxPage = $(".sec > div").length-1;
var isAnimating = false,scrollPrevent = false;

$(window).bind("scroll", function(){
	var scrTop = $(window).scrollTop(),
		num = Math.floor(scrTop / winH);
	if(num<0){
		num = 0;
	}
	critical = Math.floor(winH/3);
	var minHeight = (winH+(winH*num))-scrTop;
	
	if(timeout){clearTimeout(timeout);}   
    timeout = setTimeout(function(){ 
		if($('body').attr("class") == "fullpage" && num < maxPage){
		
		  
			if(minHeight < critical && minHeight > 20){
				animatePage(num+1);
			}
			if(minHeight < winH  && minHeight > winH-critical){
				animatePage(num);
			}
		
		}
    },100); 
	
	
	curPage = num;
	
	setrightNav(curPage);
	if(curPage == 0){
		$('.rightNav').hide();
	}else{
		$('.rightNav').show();
	}
	if(scrTop > winH*maxPage){
		$('.rightNav').hide();
		hidebottom_con();
	}else{
		showbottom_con();
	}
	
	
	if($('body').attr("class") == "fullpage" && isAnimating && scrollPrevent){
		
		
		setTimeout(function(){
			//animatePage(num+1);
		},1000);
		console.log(isAnimating)
	}
	console.log(scrTop  + ':::::'+minHeight+'====' +curPage)
	

});


 




// 以下是拖动效果

var pageWidth = 0,
    pageHeight = 0;
var lineHeight = 0, secHeight = 0;
var targetElement = null;
var movePrevent = false, touchDown = false, mousePrevent = false, wheelPrevent = true;
var animationTimeout;



function initPage(){
	pageWidth = winW;
	pageHeight = winH;
	pages = $(".sec > div");
	

    $(".sec > div").css({
        "width":"100%",//"height":$(".wrap").height()+"px"
        "height":pageHeight+"px"
    });
	
	
    //$(".wrap").height($(".wrap").height());

    secHeight = pageHeight * $(".sec > div").length;
    lineHeight = 832 * secHeight / pageHeight;

    //animatePage(curPage);
    //$(".sec, .line").removeClass("drag");

}



function prevPage(){
	var newPage = curPage - 1;
    animatePage(newPage);
    
}
function nextPage(){
	var newPage = curPage + 1;
    animatePage(newPage);
}

function animatePage( newPage ){
	isAnimating = false;
	scrollPrevent = false;
	
    if(newPage<0){
        newPage = 0;
    }
    if(newPage>$(".sec > div").length-1){
        newPage = $(".sec > div").length-1;
    }
	
	
	$('html, body').animate({scrollTop:newPage*winH},300);
	setTimeout(function(){},300);
	
	/*switch(newPage){
		case 1:
			$('.menu_pro .bg01').show();
		break;
		default:
			
	}*/
	
	
    curPage = newPage;
	
	//console.log("-----------maxPage:"+maxPage+"------curPage:"+curPage)
	
	setrightNav(curPage);
	
	
	if($('body').width() < 750){
		if(curPage == maxPage){
			$('.bottom_con').hide();
		}else{
			$('.bottom_con').show();
		}
	}
	
	
	
	
	
	

    // 每页动画
    /*if( !$(pages[curPage]).hasClass("sec0" + (curPage+1) + "_show") ){
        $(pages[curPage]).addClass("sec0" + (curPage+1) + "_show");
    }
    $(pages[curPage-1]).removeClass("sec0" + (curPage) + "_show");
    $(pages[curPage+1]).removeClass("sec0" + (curPage+2) + "_show");*/

    
}
function gotoPage(newPage){
    animatePage(newPage);
}





//点击滚动


function gonextPage(){
	if( curPage < maxPage ){
		nextPage();
	}else{
		$('html, body').animate({scrollTop:(maxPage+1)*winH},100);
	}
}

function goprevPage(){
	$('html, body').animate({scrollTop:0},100);
	$('.rightNav').show();
	showbottom_con();
}


function hidebottom_con(){
	$('.bottom_con').hide();
	$('.bottom_conUp').show();
}

function showbottom_con(){
	$('.bottom_con').show();
	$('.bottom_conUp').hide();
}



function initKeyboard(){
	$(document).keydown(function(e){
		e.preventDefault();
	    switch(e.which) {
		    case 38: // up
				//scrollPrevPage();
		    break;

		    case 40: // down
			    //scrollNextPage();
		    break;

		    default: return; // exit this handler for other keys
	    }
    });
}



function setrightNav(value){
	var obj = $('.rightNav'),
		val = value || "0";
	if((val+1)>obj.find('li').length){
		val = obj.find('li').length-1;
	}
	obj.find('a').removeClass("active");
	obj.find('li').eq(val).find('a').addClass('active');
	obj.css({"margin-top":(-obj.height()*0.5)+"px"});
}






function resize() {
	$(window).resize(function () {
		winW = $(window).width();
		winH = $(window).height();
		if($('body').width() > 750){
			setTimeout(function(){$('header .menu').attr('style','');$('.mini_nav').removeClass('open');},300);
		}
		initPage();
		setrightNav();
	});
}

function gotop(){
	$('html, body').animate({scrollTop:0},300);
}


function gotopFooter(){
	$('html, body').animate({scrollTop:0},300);
	
}




//////

var isBig = false;

function setbp(value,num){
	if($('body').width() < 750){
		return;
	}
	var obj = $('.'+value);
	isBig = true;
	$('.bigPic').show();
	obj.parent().find('img').hide();
	obj.parent().parent().find('li').removeClass('active');
	obj.show();
	obj.parent().parent().find('li').eq(num).addClass('active');
	
	picRun(value);
	
	
}
function picRun(value){
	
	var mouseX = event.clientX,
    	mouseY = event.clientY,
		obj = $('.'+value),
		imgW = obj.width(),
		imgH = obj.height(),
		mar_l = "",
		winTop = $(window).scrollTop();
	
	function prun(){
		if(isBig){
			obj.mousemove(function(e) {  
				mouseX = e.pageX;
				mouseY = e.pageY - winTop;
			 
				if(obj.width()<winW){
					mar_l = "";
				}else{
					mar_l = -(imgW - winW)*(mouseX/winW);
				}
				if(obj.height()<winH){
					mar_t = (winH-imgH)*0.5;
				}else{
					mar_t = -(imgH-winH)*(mouseY/winH);
				}
				
				obj.css({
					"margin-top":mar_t+"px",
					"margin-left":mar_l+"px"
				});
				
			});
		}
	}
	prun();
	
}

function closebp(){
	isBig = false;
	$('.bigPic').hide();
}



///////
window.onload = function(){
	resize();
	setTimeout(function(){
		isloading = false;
		$('#loading').hide();
	},30);
	
	initPage();
	setrightNav();
	
	
	
	if(curPage == 0){
		$('.rightNav').hide();
	}else{
		$('.rightNav').show();
	}
	
};
